package practise;


import java.util.*;

class Nums {
    public int val;
    public int leftIndex;
    public int rightIndex;

    public Nums(int val, int leftIndex) {
        this.val = val;
        this.leftIndex = leftIndex;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Nums nums = (Nums) o;
        return val == nums.val;
    }

    @Override
    public int hashCode() {
        return Objects.hash(val);
    }
}
public class ContainsNearbyDuplicate {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer, Nums> map = new HashMap<>();
        for(int i = 0; i < nums.length; i++) {
            Nums nums1 = map.get(nums[i]);
            if(nums1 == null) {
                map.put(nums[i], new Nums(nums[i], i));
            } else {
                if(Math.abs(i - nums1.leftIndex) <= k) {
                    return true;
                }
                nums1.leftIndex = i;
            }
        }

        return false;
    }
}
